<?php
/**
 *  礼物订单类
 *
 * */
namespace app\admin\controller;

use app\common\model\Competitor;
use think\helper\Time;

class Order extends Base
{
    protected $orderModel;
    protected $activityModel;

    protected function initialize()
    {
        parent::initialize();
        $this->orderModel = \app\common\model\Order::getInstance();
        $this->activityModel = \app\common\model\Activity::getInstance();
    }

    /**
     * 订单列表页面
     *
     * */
    public function listTpl()
    {
        $where = [];
        $list = $this->orderModel->alias('a')->field('a.*,b.title')->join('activity b', 'b.id=a.activity_id')->where($where)->paginate(20, false, ['query' => request()->param()]);

        $this->assign('list', $list);
        return $this->fetch('listtpl');
    }

    /**
     * 全部礼物记录
     * @return mixed
     * @throws \ip2region\Exception
     */
    public function newgifts()
    {
        $params = $this->request->param();
        $where = [];
        $keyword = isset($params['keyword']) ? $params['keyword'] : '';
        if (!empty($keyword)) {
            $where[] = ['a.title', 'like', '%' . urldecode($keyword) . '%'];
        }
        $lists = $this->orderModel->alias('o')
            ->where($where)
            ->field('o.*,a.title,c.name as competitor_name,c.competitor_num,c.first_image')
            ->join("activity a", "o.activity_id = a.id")
            ->join("competitor c", "o.competitor_id = c.id")
            ->paginate(20, false, ['query' => $this->request->param()]);
        $ip_obj = new \ip2region\Ip2Region('ip2region.db');
        foreach ($lists as $k => $item) {
            $item['create_time'] = formatTime($item['create_time']);
            $ip_info = $ip_obj->binarySearch($item['ip']);
            $ip_array = explode('|', $ip_info['region']);
            $item['last_ip'] = isset($ip_info['region']) ? $item['ip'] . '/' . $ip_array[2] . $ip_array[3] . $ip_array[4] : $item['ip'];
            $lists[$k] = $item;
        }

        $info = [];
        //礼物金额
        $info['total_price'] = $this->orderModel->where(['status' => 1])->sum('price');
        $info['keyword'] = $keyword;
        $this->assign('lists', $lists);
        $this->assign($info);
        return $this->fetch();
    }

    /**
     * 全部礼物记录
     * @return mixed
     * @throws \ip2region\Exception
     */
    public function gifts()
    {
        $params = $this->request->param();
        $where = [];
        $keyword = $this->request->has('keyword') ? $this->request->param('keyword') : '';
        if (!empty($keyword)) {
            $where[] = ['a.title', 'like', '%' . urldecode($keyword) . '%'];
        }
        //时间选择
        $day = isset($params['day']) ? $params['day'] : 'all';
        //时间段选择
        $btime = isset($params['btime']) ? $params['btime'] : '';
        if (empty($btime)) {
            $params['day'] = $day;
            switch ($day) {
                case 'today':
                    list($start_time, $end_time) = Time::today();
                    $where[] = ['o.create_time', 'between', [$start_time, $end_time]];
                    break;
                case 'yestoday':
                    list($start_time, $end_time) = Time::yesterday();
                    $where[] = ['o.create_time', 'between', [$start_time, $end_time]];
                    break;
                case 'three':
                    list($start_time, $end_time) = Time::dayToNow(3);
                    $where[] = ['o.create_time', 'between', [$start_time, $end_time]];
                    break;
                case 'week':
                    list($start_time, $end_time) = Time::dayToNow(7);
                    $where[] = ['o.create_time', 'between', [$start_time, $end_time]];
                    break;
                default:
                    break;
            }
        } else {
            $btime_array = explode(' - ', $btime);
            $where[] = ['o.create_time', 'between', [strtotime($btime_array[0]), strtotime($btime_array[1]) + 86399]];
        }
        //时间选择
        $type = isset($params['type']) ? $params['type'] : '0';
        $info = [];
        $info['keyword'] = $keyword;
        $info['day'] = $day;
        $info['btime'] = $btime;
        $info['type'] = $type;

        $lists = $this->orderModel->alias('o')
            ->where($where)
            ->field('o.*,a.title,c.name as competitor_name,c.competitor_num,c.first_image')
            ->join("activity a", "o.activity_id = a.id")
            ->join("competitor c", "o.competitor_id = c.id")
            ->paginate(20, false, ['query' => $this->request->param()]);
        $ip_obj = new \ip2region\Ip2Region('ip2region.db');
        foreach ($lists as $k => $item) {
            $item['create_time'] = formatTime($item['create_time']);
            $ip_info = $ip_obj->binarySearch($item['ip']);
            $ip_array = explode('|', $ip_info['region']);
            $item['last_ip'] = isset($ip_info['region']) ? $item['ip'] . '/' . $ip_array[2] . $ip_array[3] . $ip_array[4] : $item['ip'];
            $lists[$k] = $item;
        }

        $where[] = ['o.status', '=', 1];
        $lists1 = $this->orderModel->alias('o')
            ->where($where)
            ->field('o.*,a.title,c.name as competitor_name,c.competitor_num,c.first_image')
            ->join("activity a", "o.activity_id = a.id")
            ->join("competitor c", "o.competitor_id = c.id")
            ->paginate(20, false, ['query' => $this->request->param()]);
        foreach ($lists1 as $k => $item) {
            $item['create_time'] = formatTime($item['create_time']);
            $ip_info = $ip_obj->binarySearch($item['ip']);
            $ip_array = explode('|', $ip_info['region']);
            $item['last_ip'] = isset($ip_info['region']) ? $item['ip'] . '/' . $ip_array[2] . $ip_array[3] . $ip_array[4] : $item['ip'];
            $lists1[$k] = $item;
        }

        $where[] = ['o.status', '=', 0];
        $lists2 = $this->orderModel->alias('o')
            ->where($where)
            ->field('o.*,a.title,c.name as competitor_name,c.competitor_num,c.first_image')
            ->join("activity a", "o.activity_id = a.id")
            ->join("competitor c", "o.competitor_id = c.id")
            ->paginate(20, false, ['query' => $this->request->param()]);
        foreach ($lists2 as $k => $item) {
            $item['create_time'] = formatTime($item['create_time']);
            $ip_info = $ip_obj->binarySearch($item['ip']);
            $ip_array = explode('|', $ip_info['region']);
            $item['last_ip'] = isset($ip_info['region']) ? $item['ip'] . '/' . $ip_array[2] . $ip_array[3] . $ip_array[4] : $item['ip'];
            $lists2[$k] = $item;
        }

        //礼物金额
        $total_price = $this->orderModel->where(['status' => 1])->sum('price');
        $info['total_price'] = formatNumber($total_price, 2);
        $this->assign('lists', $lists);
        $this->assign('lists1', $lists1);
        $this->assign('lists2', $lists2);
        $this->assign($info);
        return $this->fetch();
    }


}